import { FormSchema } from '@/components/Form';
import { MemberSumma, renderImage, renderPopoverMemberSumma } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { cloneDeep } from 'lodash-es';
import { ref } from 'vue';

export class State {
  public id = 0; // id
  public orderId = 0; // 订单 id
  public memberId = 0; // 核销用户 id
  public productId = 0; // 核销产品
  public storeId = 0; // 核销商家 id
  public count = 0; // 核销数量
  public createdBy = 0; // 核销人
  public createdBySumma?: null | MemberSumma = null; // 核销人摘要信息
  public createdAt = ''; // 核销时间
  public productMainImage = [];

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  orderId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入订单 id',
  },
  memberId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入核销用户 id',
  },
  productId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入核销产品',
  },
  storeId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入核销商家 id',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'adminMemberMobile',
    component: 'NInput',
    label: '用户手机',
    componentProps: {
      placeholder: '请输入用户手机号码',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'productName',
    component: 'NInput',
    label: '商品名称',
    componentProps: {
      placeholder: '请输入商品名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '核销时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'orderUid',
    component: 'NInput',
    label: '订单UUID',
    componentProps: {
      placeholder: '请输入订单UUID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'orderTraceId',
    component: 'NInput',
    label: '交易ID',
    componentProps: {
      placeholder: '请输入交易ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'storeName',
    component: 'NInput',
    label: '门店名称',
    componentProps: {
      placeholder: '请输入门店名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'storePhone',
    component: 'NInput',
    label: '门店电话',
    componentProps: {
      placeholder: '请输入门店电话',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },

  {
    field: 'adminMemberRealName',
    component: 'NInput',
    label: '真实姓名',
    componentProps: {
      placeholder: '请输入真实姓名',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },

]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 60,
  },
  {
    title: '商品主图',
    key: 'productMainImage',
    align: 'center',
    width: 80,
    render(row: State) {
        return renderImage(row.productMainImage? row.productMainImage[0] : "")
      },
    // render(row: any) {
    //   const images = row.productMainImage;
    //   return h('div', [
    //     images.length > 0 && h('img', {
    //       src: images[0],
    //       style: {
    //         width: '100px',
    //       }
    //     }),
    //     h('div', row.productName)
    //   ]);
    // },
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: 280,
  },
  {
    title: '核销数量',
    key: 'count',
    align: 'center',
    width: 80,
  },
  {
    title: '用户昵称',
    key: 'adminMemberNickname',
    align: 'left',
    width: -1,
  },
  {
    title: '用户手机号',
    key: 'adminMemberMobile',
    align: 'left',
    width: -1,
  },
  {
    title: '核销人',
    key: 'createdBy',
    align: 'left',
    width: 250,
    render(row: State) {
      return renderPopoverMemberSumma(row.createdBySumma, 'mobile');
    },
  },
  {
    title: '门店名称',
    key: 'storeName',
    align: 'left',
    width: 200,
  },
  {
    title: '门店电话',
    key: 'storePhone',
    align: 'left',
    width: -1,
  },
  {
    title: '核销时间',
    key: 'createdAt',
    align: 'left',
    width: 180,
  },
  {
    title: '门店地址',
    key: 'storeAddress',
    align: 'left',
    width: -1,
  },
  {
    title: '订单UUID',
    key: 'orderUid',
    align: 'left',
    width: -1,
  },
  // {
  //   title: '用户真实姓名',
  //   key: 'adminMemberRealName',
  //   align: 'left',
  //   width: -1,
  // },
 

];